//
//  CZHNavagationBarTranslucentTool.m
//  CZHGradientNavigationController
//
//  Created by 程召华 on 2018/1/3.
//  Copyright © 2018年 程召华. All rights reserved.
//
#define CZHNavigationBarHeight 64
#define CZHNavigationBarNormalTitleColor  CZHColor(0x333333)
#define CZHNavigationBarTitleFont CZHGlobelBoldFont(17)
///半透明透明的时候文字颜色
#define CZHNavigationBarTranslucentTitleColor  CZHColor(0xffffff)
#define CZHGlobelBoldFont(__VA_ARGS__) ([UIFont boldSystemFontOfSize:CZH_ScaleFont(__VA_ARGS__)])
#define CZH_ScaleFont(__VA_ARGS__)  ([UIScreen mainScreen].bounds.size.width/375)*(__VA_ARGS__)

/**颜色*/
#define CZHColor(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define CZHRGBColor(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

#import "CZHNavagationBarTranslucentTool.h"
#import "UIColor+CZHExtension.h"

@implementation CZHNavagationBarTranslucentTool

+ (void)czh_changeNavigationBarTransparencyWithViewController:(UIViewController *)viewController contentOffsetY:(CGFloat)contentOffsetY {
    
    if (contentOffsetY >= CZHNavigationBarHeight) {
        //滑动超过偏移值，显示原本的颜色
        [viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:CZHNavigationBarNormalTitleColor,NSForegroundColorAttributeName, CZHNavigationBarTitleFont,NSFontAttributeName,nil]];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        [viewController.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [viewController.navigationController.navigationBar setShadowImage:nil];
    } else if (contentOffsetY < CZHNavigationBarHeight && contentOffsetY > 0) {
        //滑动在偏移区间之内，显示过渡色
        [viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:CZHNavigationBarNormalTitleColor,NSForegroundColorAttributeName, CZHNavigationBarTitleFont,NSFontAttributeName,nil]];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        CGFloat alpha = contentOffsetY / CZHNavigationBarHeight;
        [viewController.navigationController.navigationBar setBackgroundImage:[UIColor imageWithColor:CZHRGBColor(0xffffff, alpha)] forBarMetrics:UIBarMetricsDefault];
        [viewController.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    } else {
        //未滑动，显示初始色
        [viewController.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:CZHNavigationBarTranslucentTitleColor,NSForegroundColorAttributeName, CZHNavigationBarTitleFont,NSFontAttributeName,nil]];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        [viewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"naviBackImage"] forBarMetrics:UIBarMetricsDefault];
//        [viewController.navigationController.navigationBar setBackgroundImage:[UIColor imageWithColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]] forBarMetrics:UIBarMetricsDefault];
        [viewController.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
     
}

@end
